package com.example.wwj.controller;


import com.example.wwj.mapper.CourseMapper;
import com.example.wwj.mapper.CurriculumMapper;
import com.example.wwj.pojo.Course;
import com.example.wwj.pojo.Curriculum;
import com.example.wwj.pojo.Student;
import com.example.wwj.service.CurriculumService;
import com.example.wwj.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/curr")
public class curriculumController {

    @Autowired
    private CurriculumService curriculumService;

    /*
    * 查询课程信息
    * 可用
    * */
    @GetMapping("/course")
    public List<Course> getCourseList(){
        return curriculumService.courseList();
    }
    /*
    * 根据课程id查询课程信息
    * course_id:课程id
    * 可用
    * */

    @GetMapping("/courseById")
    public List<Course> getCourseById(@RequestParam Integer course_id){
        return curriculumService.getCourseById(course_id);
    }

    /*
    * 查询所有的课表信息
    * 可用
    * */
    @GetMapping("/curriculum")
    public List<Curriculum> getCurriculumList(){
        return curriculumService.curriculumList();
    }

    /*
    * 根据老师id查询对应的课表信息
    * teacher_id:老师id
    * 可用
    * */
    @GetMapping("/getcurriculumbyid")
    public List<Curriculum>  getCurriculumByTeacherId(@RequestParam Integer teacher_id){
        return  curriculumService.findCurriculumByTeacherId(teacher_id);
    }

   /*
    @PathVariable:      从后端向前端传值
    @PathParam("id") ： 通过url接收前端传递的id值  ，例子： @GetMapping("/getcurriculumbyid/{id}")

    @RequestParam:      跟上面的 @PathParam("id") 类似，作用相同，但是路径后不接{id},
                        例子： @GetMapping("/getcurriculumbyid")

    @RequestBody：       mvc能自动将请求体的数据转化为对象形式，常用于处理post请求，
                        例如登录注册，接收的username，password会被自动转化为对象，存入User对象里面，进行后续的sql语句或者判断
                        通过user.getUsername(),user.setUsername()等方法调用
    */

    /**
     * 课表信息分页查询
     * pageNum:页码
     * pageSize:一页几个数据
     * total：数据总数
     * classId:班级id
     * gradeId:年级id
     * teacher_name:老师姓名
     */
    @GetMapping("/page")
    public Map<String, Object> findPage(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) Integer class_id,
            @RequestParam(required = false) Integer grade_id,
            @RequestParam(required = false) String teacher_name
    ) {
        if (pageNum == null || pageSize == null) {
            // 处理缺少参数的情况，这里可以返回错误信息或者设置默认值
        }

        pageNum = (pageNum - 1) * pageSize;

        List<Curriculum> data;
        Integer total;

        if (class_id != null || grade_id != null || (teacher_name != null && !teacher_name.isEmpty())) {
            data = curriculumService.selectPage(pageNum, pageSize, class_id, grade_id, teacher_name);
            total = curriculumService.selectTotal(class_id, grade_id, teacher_name);
        } else {
            data = curriculumService.selectPage(pageNum, pageSize, null, null, null); // 查询所有数据
            total = curriculumService.selectTotal(class_id, grade_id, teacher_name);
        }

        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }
    /*
     * 批量删除
     * ids:学生的id的列表
     * */
    @GetMapping("/delete")
    public int delete(@RequestParam("ids") List<Integer> ids) {
        return curriculumService.deleteById(ids);
    }

}
